import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { ArticleInfoEntity } from '../../entity/info';
import { ArticleInfoService } from '../../service/info';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';
import { ArticleCategoryEntity } from '../../entity/category';
import { Get } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';

/**
 * 文章管理
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: ArticleInfoEntity,
  service: ArticleInfoService,
  pageQueryOp: {
    keyWordLikeFields: ['a.title'],
    fieldEq: ['a.categoryId', 'a.status', 'a.authorId'],
    select: ['a.*', 'b.name as categoryName', 'c.nickName as authorName'],
    join: [
      {
        entity: ArticleCategoryEntity,
        alias: 'b',
        condition: 'a.categoryId = b.id',
        type: 'leftJoin',
      },
      {
        entity: BaseSysUserEntity,
        alias: 'c',
        condition: 'a.authorId = c.id',
        type: 'leftJoin',
      },
    ],
  },
})
export class AdminArticleInfoController extends BaseController {
  @Inject()
  articleInfoService: ArticleInfoService;

  @Inject()
  ctx: Context;

  @Get('/view', { summary: '查看帖子' })
  async view() {
    const { id } = this.ctx.request.query;
    await this.articleInfoService.view(Number(id));
    return this.ok();
  }

  @Get('/like', { summary: '点赞帖子' })
  async like() {
    const { id } = this.ctx.request.query;
    await this.articleInfoService.like(Number(id));
    return this.ok();
  }
}
